package com.aizhixin.lab.course.template.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.course.template.domain.TemplateChapterDomain;
import com.aizhixin.lab.course.template.dto.*;
import com.aizhixin.lab.course.template.entity.TemplateChapter;
import com.aizhixin.lab.course.template.entity.TemplateChapterFile;
import com.aizhixin.lab.course.template.service.TemplateChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/course/template/chapter")
@Api(value = "课程模板章节相关API")
public class TemplateChapterController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private TemplateChapterService chapterService;

    @RequestMapping(value = "/getList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程章节", response = Void.class, notes = "查询课程章节<br>@author hsh")
    public ResponseEntity<?> getList(@RequestHeader("Authorization") String token,
                                     @ApiParam(value = "templateId", required = false) @RequestParam(value = "templateId", required = false) Long templateId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<TemplateChapter> result = chapterService.getList(templateId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getListWithFile", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程章节包含文件", response = Void.class, notes = "查询课程章节包含文件<br>@author hsh")
    public ResponseEntity<?> getListWithFile(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "templateId", required = false) @RequestParam(value = "templateId", required = false) Long templateId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<TemplateChapterDomain> result = chapterService.getListWithFile(templateId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "添加课程章节", response = Void.class, notes = "添加课程章节<br>@author hsh")
    public ResponseEntity<?> add(@RequestHeader("Authorization") String token,
                                 @ApiParam(value = "chapter type 10:章 20:节", required = false) @RequestBody TemplateChapterAddDTO dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = chapterService.add(dto);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "保存课程章节", response = Void.class, notes = "保存课程章节<br>@author hsh")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token,
                                  @ApiParam(value = "chapter", required = false) @RequestBody TemplateChapterDTO dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = chapterService.save(dto);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/saveSort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "保存课程章节排序", response = Void.class, notes = "保存课程章节排序<br>@author hsh")
    public ResponseEntity<?> saveSort(@RequestHeader("Authorization") String token,
                                      @ApiParam(value = "chapter", required = false) @RequestBody TemplateChapterSortDTO dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = chapterService.saveSort(dto);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "DELETE", value = "删除课程章节", response = Void.class, notes = "删除课程章节<br>@author hsh")
    public ResponseEntity<?> delete(@RequestHeader("Authorization") String token,
                                    @ApiParam(value = "chapterId", required = false) @RequestParam(value = "chapterId", required = false) Long chapterId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = chapterService.delete(chapterId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getFileList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程章节文件", response = Void.class, notes = "查询课程章节文件<br>@author hsh")
    public ResponseEntity<?> getFileList(@RequestHeader("Authorization") String token,
                                         @ApiParam(value = "chapterId", required = false) @RequestParam(value = "chapterId", required = false) Long chapterId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<TemplateChapterFile> result = chapterService.getFileList(chapterId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/saveFile", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "保存文件", response = Void.class, notes = "保存文件<br>@author hsh")
    public ResponseEntity<?> saveFile(@RequestHeader("Authorization") String token,
                                      @ApiParam(value = "type 10:ppt 20:视频 30:任务手册 40:实验指导手册 80:教学设计", required = false) @RequestBody TemplateChapterFileDTO dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isEmpty(dto.getChapterId())) {
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR, "未选择章节");
            return new ResponseEntity(result, HttpStatus.OK);
        }
        result = chapterService.saveFile(dto);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/saveFileOrder", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "保存文件顺序", response = Void.class, notes = "保存文件顺序<br>@author hsh")
    public ResponseEntity<?> saveFileOrder(@RequestHeader("Authorization") String token,
                                           @ApiParam(value = "list", required = false) @RequestBody TemplateChapterFileOrderListDTO dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = chapterService.saveFileOrder(dto);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/deleteFile", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "DELETE", value = "删除课程章节文件", response = Void.class, notes = "删除课程章节文件<br>@author hsh")
    public ResponseEntity<?> deleteFile(@RequestHeader("Authorization") String token,
                                        @ApiParam(value = "fileId", required = false) @RequestParam(value = "fileId", required = false) Long fileId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = chapterService.deleteFile(fileId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findChapterByTemplateId", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程下的章", response = Void.class, notes = "查询课程下的章<br><br><b>@author lwq</b>")
    public ResponseEntity<?> findChapterByTemplateId(@RequestHeader("Authorization") String token,
                                                     @ApiParam(value = "templateId", required = true) @RequestParam(value = "templateId") Long templateId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<TemplateChapter> list = chapterService.getList(templateId);
        return new ResponseEntity(list, HttpStatus.OK);
    }
    @RequestMapping(value = "/findChapterByChapterId", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询章下的节", response = Void.class, notes = "查询章下的节<br><br><b>@author lwq</b>")
    public ResponseEntity<?> findChapterByChapterId(@RequestHeader("Authorization") String token,
                                                     @ApiParam(value = "chapterId", required = true) @RequestParam(value = "chapterId") Long chapterId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<TemplateChapter> list = chapterService.findChapterByChapterId(chapterId);
        return new ResponseEntity(list, HttpStatus.OK);
    }
}
